def f(n):
    return n * (3 * n - 1) / 2

p = [f(n) for n in xrange(1, 5000)]
sp = {}
for pp in p:
    sp[pp] = 1

n = len(p)
ans = 2 ** 32
for i in xrange(n):
    for j in xrange(i + 1, n):
        s = p[i] + p[j]
        k = p[j] - p[i]
        if s in sp and k in sp:
            if ans > k:
                ans = k

print ans




